from PySide6.QtCore import QDateTime, QTime
from PySide6.QtWidgets import QApplication, QWidget, QMessageBox
from file_py.SqlHelper import SqlHelper
from ui.delete_ui import Ui_Form


class DelData(QWidget):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Form()
        self.ui.setupUi(self)
        # 获取当前日期时间
        current_date_time = QDateTime.currentDateTime()
        # 设置日期为今天，同时将小时、分钟和秒设置为0
        current_date_time.setDate(current_date_time.date())
        current_date_time.setTime(QTime(0, 0, 0))
        self.ui.Sdate.setDateTime(current_date_time)
        current_date_time.setTime(QTime(23, 59, 59))
        self.ui.Edate.setDateTime(current_date_time)
        self.ui.btn_Del.clicked.connect(self.del_data)
        self.sqlHelper = SqlHelper()

    def wrapper(func):
        def inner(*args, **kwargs):
            if QMessageBox.warning(None, "系统消息", "您点击了" + func.__name__ + "按钮，请确认是否要" + func.__name__,
                                   QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes:
                func(*args, **kwargs)

        return inner

    @wrapper
    def del_data(self):
        start_date = self.ui.Sdate.dateTime().toString("yyyy-MM-dd HH:mm:ss")
        end_date = self.ui.Edate.dateTime().toString("yyyy-MM-dd HH:mm:ss")
        sql = f"DELETE Webasto WHERE importTime>='{start_date}' AND importTime<='{end_date}'"
        if self.sqlHelper.delete_from_sql(sql) > 0:
            QMessageBox.information(None, "系统消息", "删除成功")
        else:
            QMessageBox.warning(None, "系统消息", "删除失败")
